import service.ComputerService;
import service.CustomerService;
import service.SaleService;
import service.UserService;
import db.DatabaseManager;
import util.SessionManager;
import java.util.Scanner;

public class Main {
    private static ComputerService computerService;
    private static CustomerService customerService;
    private static SaleService saleService;
    private static UserService userService;
    private static Scanner scanner;

    public static void main(String[] args) {
        System.out.println("启动电脑销售管理系统...");
        System.out.println("==========================================");

        // 初始化数据库
        try {
            DatabaseManager.initializeDatabase();
            System.out.println("数据库初始化成功！");
        } catch (Exception e) {
            System.out.println("数据库初始化失败: " + e.getMessage());
            return;
        }

        initializeServices();

        // 用户登录
        if (!loginProcess()) {
            System.out.println("登录失败，系统退出");
            return;
        }

        // 显示主菜单
        showMainMenu();
    }

    private static void initializeServices() {
        computerService = new ComputerService();
        customerService = new CustomerService();
        saleService = new SaleService(computerService, customerService);
        userService = new UserService();
        scanner = new Scanner(System.in);
    }

    private static boolean loginProcess() {
        System.out.println("\n===== 用户登录 =====");
        System.out.println("默认账户: admin/admin123, manager/manager123, staff/staff123");

        return UserService.login();
    }

    private static void showMainMenu() {
        while (true) {
            System.out.println("\n===== 电脑销售管理系统 =====");
            System.out.println("当前用户: " + SessionManager.getCurrentUsername() +
                    " [" + SessionManager.getCurrentRole() + "]");
            System.out.println("==========================================");
            System.out.println("1.电脑管理");
            System.out.println("2.客户管理");
            System.out.println("3.销售管理");
            System.out.println("4.用户管理");
            System.out.println("5.账户设置");
            System.out.println("6.系统工具");
            System.out.println("7.退出系统");
            System.out.println("==========================================");
            System.out.print("请选择操作: ");

            int choice = getIntInput();

            switch (choice) {
                case 1:
                    showComputerMenu();
                    break;
                case 2:
                    showCustomerMenu();
                    break;
                case 3:
                    showSaleMenu();
                    break;
                case 4:
                    showUserMenu();
                    break;
                case 5:
                    showAccountMenu();
                    break;
                case 6:
                    showSystemToolsMenu();
                    break;
                case 7:
                    logoutAndExit();
                    return;
                default:
                    System.out.println("无效选择，请重新输入！");
            }
        }
    }

    private static void showComputerMenu() {
        while (true) {
            System.out.println("\n===== 电脑管理 =====");
            System.out.println("1.添加电脑");
            System.out.println("2.查看所有电脑");
            System.out.println("3.更新库存");
            System.out.println("4.返回主菜单");
            System.out.print("请选择操作: ");

            int choice = getIntInput();

            switch (choice) {
                case 1:
                    computerService.addComputer();
                    break;
                case 2:
                    computerService.viewAllComputers();
                    break;
                case 3:
                    computerService.updateComputerStock();
                    break;
                case 4:
                    return;
                default:
                    System.out.println("无效选择，请重新输入！");
            }
        }
    }

    private static void showCustomerMenu() {
        while (true) {
            System.out.println("\n===== 客户管理 =====");
            System.out.println("1.添加客户");
            System.out.println("2.查看所有客户");
            System.out.println("3.返回主菜单");
            System.out.print("请选择操作: ");

            int choice = getIntInput();

            switch (choice) {
                case 1:
                    customerService.addCustomer();
                    break;
                case 2:
                    customerService.viewAllCustomers();
                    break;
                case 3:
                    return;
                default:
                    System.out.println("无效选择，请重新输入！");
            }
        }
    }

    private static void showSaleMenu() {
        while (true) {
            System.out.println("\n===== 销售管理 =====");
            System.out.println("1.进行销售");
            System.out.println("2.查看销售记录");
            System.out.println("3.销售统计报告");
            System.out.println("4.返回主菜单");
            System.out.print("请选择操作: ");

            int choice = getIntInput();

            switch (choice) {
                case 1:
                    saleService.makeSale();
                    break;
                case 2:
                    saleService.viewAllSales();
                    break;
                case 3:
                    saleService.viewSalesReport();
                    break;
                case 4:
                    return;
                default:
                    System.out.println("无效选择，请重新输入！");
            }
        }
    }

    private static void showUserMenu() {
        if (!SessionManager.isAdmin()) {
            System.out.println("权限不足，需要管理员权限！");
            return;
        }

        while (true) {
            System.out.println("\n===== 用户管理 =====");
            System.out.println("1.添加用户");
            System.out.println("2.查看所有用户");
            System.out.println("3.返回主菜单");
            System.out.print("请选择操作: ");

            int choice = getIntInput();

            switch (choice) {
                case 1:
                    userService.addUser();
                    break;
                case 2:
                    userService.viewAllUsers();
                    break;
                case 3:
                    return;
                default:
                    System.out.println("无效选择，请重新输入！");
            }
        }
    }

    private static void showAccountMenu() {
        while (true) {
            System.out.println("\n===== 账户设置 =====");
            System.out.println("1.修改密码");
            System.out.println("2.查看个人信息");
            System.out.println("3.返回主菜单");
            System.out.print("请选择操作: ");

            int choice = getIntInput();

            switch (choice) {
                case 1:
                    userService.changePassword();
                    break;
                case 2:
                    userService.showCurrentUser();
                    break;
                case 3:
                    return;
                default:
                    System.out.println("无效选择，请重新输入！");
            }
        }
    }

    private static void showSystemToolsMenu() {
        if (!SessionManager.isAdmin()) {
            System.out.println("权限不足，需要管理员权限！");
            return;
        }

        while (true) {
            System.out.println("\n===== 系统工具 =====");
            System.out.println("1.数据库状态");
            System.out.println("2.返回主菜单");
            System.out.print("请选择操作: ");

            int choice = getIntInput();

            switch (choice) {
                case 1:
                    DatabaseManager.showDatabaseStatus();
                    break;
                case 2:
                    return;
                default:
                    System.out.println("无效选择，请重新输入！");
            }
        }
    }

    private static void logoutAndExit() {
        System.out.println("\n===== 退出系统 =====");
        userService.logout();
        System.out.println("已安全退出系统");
        System.out.println("感谢使用电脑销售管理系统！再见！");
    }

    private static int getIntInput() {
        while (true) {
            try {
                int input = scanner.nextInt();
                scanner.nextLine();
                return input;
            } catch (Exception e) {
                System.out.print("请输入有效数字: ");
                scanner.nextLine();
            }
        }
    }
}